import Taro, { Component } from '@tarojs/taro'
import { Swiper, SwiperItem, Image, View, Text ,RichText } from '@tarojs/components'
import CommonJs from '../../utils/common'
import '../../assets/common.scss'

export default class Gwlist extends Component {
 constructor(props) {
    super(props);
    this.state = {
		showgw:0,
		gwinfo:{consultant:''},
		weblogo:CommonJs.getlogo()
    }
  }

  componentWillMount () {
  	var _this=this;
  	this.getgw();
  }
  componentDidShow () {
	var dateinfo=Taro.getStorageSync('dateinfo')
    if(dateinfo.token){
		this.getgw();
	}
  }
  shouldComponentUpdate () {
  }
  componentWillUnmount () {

  }
   chat(){
  	var gwinfo=this.state.gwinfo;
  	this.$preload({
	    'friendId':gwinfo.consultant.id,
	    'name':gwinfo.consultant.accountName,
	    'byfrom':'guwen'
	  });
  	CommonJs.openTo("/pages/chat/chat");
  }
  getgw(){
  	var _this=this;
  	var cid=Taro.getStorageSync('dateinfo').qid;
  	if(cid){
	  	var url="/crmweb/papi/v1.9/customer-shop/findByShopIdAndUserId/"+cid
	  	CommonJs.ajaxRequest(url,'','GET',function(res){
	  		//console.log(JSON.stringify(res))
	  		if(res.data){
	  			//Taro.setStorage({key:'shopId',data:res.data.consultant.shopId})
				_this.setState({
			     	gwinfo:res.data,
			     	showgw:1
			    })
			}
	  	})
	}
  	
  }
  bdgw(){
  	var _this=this;
  	var url="/crmweb/papi/v1.9/customer-shop/crate/customerShop?consultantId="+this.state.gwinfo.consultant.id
  	CommonJs.ajaxRequest(url,'','POST',function(res){
  		console.log(JSON.stringify(res))
  		Taro.showToast({
        title:'成功绑定，界面跳转中..',
        icon: 'none',
        duration: 2000
      });
      setTimeout(function(){
      	_this.chat();
      },2000)
  		
  	},"wfw")
  }
  render () {
	const gwinfo = this.state.gwinfo
	const logo=this.state.weblogo
    return(
    	<View className={this.state.showgw==1?"gwlist gwlistcur":"gwlist"} style={this.state.showgw==1?'':'display:none'}>
        	<View className='gw-dl' style={gwinfo.consultant?'':'display:none'} onClick={this.chat}>
        	  <View className='gw-dl-tx'>
        			<Image className='tx' mode='widthFix' src={gwinfo.headPortrait?gwinfo.headPortrait:logo}></Image>
        		</View>
        		<View className='gw-dt'>联系TA</View>
        		<View className='gw-dd'>
        			<Text className='gw-dd-tit'>亲,我是你的专属顾问{gwinfo.consultant.accountName}</Text>
        			<Text className='gw-dd-span'>{gwinfo.shopName}</Text>
        		</View>
        		<View className='gw-num' style={gwinfo.unreadMessage>0?'':'display:none;'}>{gwinfo.unreadMessage}</View>
        	</View>
        	
        	<View className='gw-dl' style={gwinfo.consultant?'display:none':''}  onClick={this.bdgw}>
        	  <View className='gw-dl-tx'>
        			<Image className='tx' mode='widthFix' src={gwinfo.headPortrait?gwinfo.headPortrait:logo}></Image>
        		</View>
        		<View className='gw-dt'>绑定TA</View>
        		<View className='gw-dd'>
        			<Text className='gw-dd-tit'>专属顾问{gwinfo.consultant.accountName}</Text>
        			<Text className='gw-dd-span'>系统推荐,必属精品</Text>
        		</View>
        		<View className='gw-num' style={gwinfo.unreadMessage>0?'':'display:none;'}>{gwinfo.unreadMessage}</View>
        	</View>
        	
        </View>
    )
  }
}